package com.test.daily.leetcode.y2022.m07.day0705.v03;

import java.util.Stack;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2022/7/5 下午 01:14
 * @version: 1.0
 */
public class Solution {
    public static class MyStack1{
        Stack<Integer> dataStack;
        Stack<Integer> minStack;
        public MyStack1(){
            this.dataStack = new Stack();
            this.minStack = new Stack();
        }
        public int pop(){
            if(this.dataStack.isEmpty()){
                throw new RuntimeException("stack is empty.");
            }
            int value = this.dataStack.pop();
            if(value == this.getMin()){
                this.minStack.pop();
            }
            return value;
        }
        public void push(int data){
            if(minStack.size() == 0){
                this.minStack.push(data);
            } else {
                if(this.getMin() >= data){
                    this.minStack.push(data);
                }
            }
            this.dataStack.push(data);
        }
        public int getMin(){
            return this.minStack.peek();
        }
    }

    public static class MyStack2{
        public Stack<Integer> dataStack;
        public Stack<Integer> minStack;
        public MyStack2(){
            this.dataStack = new Stack<>();
            this.minStack = new Stack<>();
        }
        public int pop(){
            int value = this.dataStack.pop();
            this.minStack.pop();
            return value;
        }
        public void push(int value){
            if(value < this.getMin()){
                this.minStack.push(value);
            }else{
                this.minStack.push(this.getMin());
            }
            this.dataStack.push(value);
        }

        public int getMin(){
            if(minStack.isEmpty()){
                throw new RuntimeException("stack is empty");
            }
            return this.minStack.peek();
        }

    }
}
